﻿using HalconDotNet;
using OpenCvSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PLCSharp.Views.Vision.Halcon
{
   public class HalconExtension
    {
        public static bool FindLine(HObject ho_Image, int row1, int column1, int row2, int column2, int num, int mode, out Line2D line)
        {
            try
            {
                HTuple hv_LinePar = new();
                hv_LinePar = hv_LinePar.TupleConcat(row1);
                hv_LinePar = hv_LinePar.TupleConcat(column1);
                hv_LinePar = hv_LinePar.TupleConcat(row2);
                hv_LinePar = hv_LinePar.TupleConcat(column2);
                HOperatorSet.GenEmptyObj(out HObject ho_Contour_Line);
                HOperatorSet.GenEmptyObj(out HObject ho_Contours);
                HOperatorSet.GenEmptyObj(out HObject ho_Cross);
                //从白到黑：negative 从黑到白 positive
                string modestr = "negative";
                switch (mode)
                {
                    case 0:
                        modestr = "all";
                        break;
                    case 1:
                        modestr = "positive";
                        break;
                }

                HOperatorSet.CreateMetrologyModel(out HTuple hv_MetrologyHandle_Line);
                HOperatorSet.AddMetrologyObjectGeneric(hv_MetrologyHandle_Line, "line", hv_LinePar, 5, 5, 1, 10, "min_score", 0.5, out HTuple hv_Line_Index);
                HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle_Line, hv_Line_Index, "measure_select", "first");
                HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle_Line, hv_Line_Index, "measure_transition", modestr); //从白到黑：negative 从黑到白 positive
                HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle_Line, hv_Line_Index, "distance_threshold", 2);
                HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle_Line, hv_Line_Index, "measure_length1", 200);
                HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle_Line, hv_Line_Index, "measure_length2", 30);
                HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle_Line, hv_Line_Index, "num_measures", 100);

                HOperatorSet.ApplyMetrologyModel(ho_Image, hv_MetrologyHandle_Line);

                ho_Contour_Line.Dispose();
                HOperatorSet.GetMetrologyObjectResultContour(out ho_Contour_Line, hv_MetrologyHandle_Line, hv_Line_Index, "all", 1.5);

                HOperatorSet.GetMetrologyObjectResult(hv_MetrologyHandle_Line, hv_Line_Index, "all", "result_type", "row_begin", out HTuple row_begin);
                HOperatorSet.GetMetrologyObjectResult(hv_MetrologyHandle_Line, hv_Line_Index, "all", "result_type", "column_begin", out HTuple column_begin);
                HOperatorSet.GetMetrologyObjectResult(hv_MetrologyHandle_Line, hv_Line_Index, "all", "result_type", "row_end", out HTuple row_end);
                HOperatorSet.GetMetrologyObjectResult(hv_MetrologyHandle_Line, hv_Line_Index, "all", "result_type", "column_end", out HTuple column_end);

                HOperatorSet.ClearMetrologyModel(hv_MetrologyHandle_Line);


                line = new Line2D(row_begin.D, column_begin.D, row_end.D, column_end.D);

                // ShowText("像素坐标  X:"+Math.Round( hv_Rowex.D,4)+"      Y:"+Math.Round( hv_Columnex.D,4), "green");

                ho_Contour_Line.Dispose();
                ho_Contours.Dispose();
                ho_Cross.Dispose();

                return true;
            }
            catch (Exception)
            {
                line = new Line2D(0, 0, 1, 1);

                return false;
            }

        }
    }
}
